package org.jeecg.modules.haili.job;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.alibaba.fastjson.JSONObject;
import org.jeecg.modules.haili.entity.insurance.NewOrder;
import org.jeecg.modules.haili.entity.insurance.OriginInsOrder;
import org.jeecg.modules.haili.entity.response.MessageResponse;
import org.jeecg.modules.haili.service.impl.InsOrignOrderServiceImpl;
import org.jeecg.modules.haili.service.impl.NewOrderServiceImpl;
import org.jeecg.modules.haili.service.impl.UnderWritingServiceImpl;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

@DisallowConcurrentExecution
@Service
public class InsuranceJob implements Job {
    private final Integer UNDER_FLAG_SUCCESS = 0;

    private final Logger logger = LoggerFactory.getLogger(InsuranceJob.class);
    final InsOrignOrderServiceImpl originOrderService;

    final UnderWritingServiceImpl underService;

    final NewOrderServiceImpl newOrderService;

    public InsuranceJob(InsOrignOrderServiceImpl originOrderService, UnderWritingServiceImpl underService, NewOrderServiceImpl newOrderService) {
        this.originOrderService = originOrderService;
        this.underService = underService;
        this.newOrderService = newOrderService;
    }

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        logger.info("开始执行核保任务...");
        List<OriginInsOrder> originInsOrders = originOrderService.findUnInsurancedOrder();
        for (OriginInsOrder order : originInsOrders) {
            logger.info("开始核保订单,{}", JSONObject.toJSONString(order));
            newOrderService.insertOriginOrder(order);
            MessageResponse underResponse = postInsurance(order);
            if (success(underResponse)) {
                updateUnderResult(underResponse, order);
                MessageResponse chengbaoResponse = postChengbao(order, underResponse);
                if (success(chengbaoResponse)) {
                    updateChengbaoResponse(chengbaoResponse, order);
                }
            }
        }
        logger.info("执行投保任务结束。");
    }

    /**
     * 判断是否成功
     */
    private boolean success(MessageResponse response) {
        return (response.getResponse_body() != null && 0 == response.getResponse_body().getResultCode());
    }

    private void updateChengbaoResponse(MessageResponse chengbaoResponse, OriginInsOrder order) {
        order.setDanhao(chengbaoResponse.getResponse_body().getData().getPolicyNo());
        order.setPolicyno(chengbaoResponse.getResponse_body().getData().getPolicyNo());
        order.setZuheflag(chengbaoResponse.getResponse_body().getData().getZuheFlag());
        originOrderService.updateById(order);

        NewOrder newOrder = new NewOrder();
        BeanUtil.copyProperties(order, newOrder, CopyOptions.create());
        newOrder.setUnderResponse(JSONObject.toJSONString(chengbaoResponse));
        newOrder.setUnderFlag(UNDER_FLAG_SUCCESS);
        newOrderService.updateById(newOrder);
    }

    private MessageResponse postChengbao(OriginInsOrder order, MessageResponse underResponse) {
        logger.info("开始承保请求,订单id{}", order.getId());
        return underService.chengbaoOrder(order, underResponse);
    }

    /**
     * 根据结果更新数据库
     */
    private void updateUnderResult(MessageResponse underResponse, OriginInsOrder order) {
        if (underResponse.getResponse_body() == null && 0 != underResponse.getResponse_body().getResultCode()) {
            return;
        }
        order.setProposalno(underResponse.getResponse_body().getData().getProposalNo());
        order.setOrderno(underResponse.getResponse_body().getData().getOrderNo());
        //todo gengxin
        originOrderService.updateById(order);
        //
        NewOrder newOrder = new NewOrder();
        BeanUtil.copyProperties(order, newOrder, CopyOptions.create());
        newOrderService.updateById(newOrder);
    }

    /**
     * 发送报文，核保请求保单号
     */
    private MessageResponse postInsurance(OriginInsOrder order) {

        return underService.underOrder(order);
    }
}
